/**
 * Created by cjb on 2017-09-15 19:40:32.
 */

(function(){
	var loadDiv = null,postCount = 7,count = 0,intervelFun;

	$(function () {
		loadDiv = $("#loadDiv").loading();//初始化提示操作执行中
	    initAddWindow();
		initAuthDepts("dangerhousDeptIds",1);
		initAuthDepts("equipmentDeptIds",4);
		initAuthDepts("teamDeptIds",8);
		initAuthDepts("technologyDeptIds",14);
		initAuthDepts("constructDeptIds",15);
		initAuthDepts("fishboatDeptIds",18);
		initAuthDepts("warehouseDeptIds",16);
		initCheck();
	    //去除所有控件的虚线框
		removeBlur();
	});
	
	/**
	 * 初始化添加窗口
	 */
	function initAddWindow(){
		//声明消息提示
		var toolTips = new Object();

		//设置表单验证
		$("#addForm").validate({
			rules:{
			},
			messages:{
			},
			errorPlacement: function(eror,element){
				var toolTip = createTooltip(toolTips,element.attr("id"));
				toolTip.setContent(eror.text());
				toolTip.close();
				element.addClass("error-border");
			},
			success:function(label){
				var elementId = label.attr("id").substring(0,label.attr("id").length - 6);
				var toolTip = toolTips[elementId];
				$("#" + elementId).removeClass("error-border");
				toolTip.close();
				toolTip.detach();
			},
			submitHandler:function(form){
				loadDiv.showLoading();

				var formData = [];
				formData.push($("#addForm").serialize());
				$("#dangerhousDeptIds .selected:not(.check-all)").each(function(){
					formData.push("dangerousDeptIds=" + $(this).attr("value"));
				});
				$("#equipmentDeptIds .selected:not(.check-all)").each(function(){
					formData.push("equipmentDeptIds=" + $(this).attr("value"));
				});
				$("#teamDeptIds .selected:not(.check-all)").each(function(){
					formData.push("teamDeptIds=" + $(this).attr("value"));
				});
				$("#technologyDeptIds .selected:not(.check-all)").each(function(){
					formData.push("technologyDeptIds=" + $(this).attr("value"));
				});
				$("#constructDeptIds .selected:not(.check-all)").each(function(){
					formData.push("constructDeptIds=" + $(this).attr("value"));
				});
				$("#fishboatDeptIds .selected:not(.check-all)").each(function(){
					formData.push("fishboatDeptIds=" + $(this).attr("value"));
				});
				$("#warehouseDeptIds .selected:not(.check-all)").each(function(){
					formData.push("warehouseDeptIds=" + $(this).attr("value"));
				});

				post(getPath() + "app/authority/add",
					formData.join("&"),
					function (resultObject) {
						loadDiv.hideLoading();
						createWindow("更新成功", {
							url: getPath() + "base/user/list/1/" + pageSize + '?' +
							($("#isFilter").val() == 1 ? ("?" + $("#searchForm").serialize()) : "")
						}).dialog("open");
					}, function (resultObject) {
						loadDiv.hideLoading();
						createWindow(resultObject).dialog('open');
					});

			}
		});

		//隐藏列表，显示添加
		$('#dialog').show();
		$('.grid-list').hide();

		//取消
		$('#cancleBtn').click(function(){
			$('#dialog').hide();
			$('.grid-list').show();
		});
	}

	/**
	 * 初始化权限
	 */
	function initAuthDepts(divId,type){
		post(getPath() + "app/authority/findlist/" + $("#userId").val(),{type:type},
			function(resultObject){
				if(resultObject.length > 0){
					var htmlStr = [];
					htmlStr.push("<span class='checkbox check-all' value=''>全选</span>");
					var checkCount = 0;
					for(var i=0; i < resultObject.length; i++){
						var item = resultObject[i];
						htmlStr.push("<span class='checkbox " + (item.checked ? "selected" : "") + "' value='" + item.id + "'>" + item.name + "</span>");
						if(item.checked)
							checkCount++;
					}
					$("#" + divId).append(htmlStr);
					if(checkCount == resultObject.length)
						$("#" + divId + " .check-all").addClass("selected");
					count++;
				}
			}
		);
	}

	/**
	 * 初始化全选和反选
	 */
	function initCheck(){
		intervelFun = setInterval(function(){
			if(postCount == count){
				$(".checkbox:not(.check-all)").click(function(){
					if($(this).hasClass("selected")){
						$(this).removeClass("selected");
						$(this).prevAll('.check-all').removeClass('selected');
					}else{
						$(this).addClass("selected");
						var allLen = $(this).parent().find(".checkbox:not(.check-all)").length;
						var selectedLen = $(this).parent().find(".selected:not(.check-all)").length;
						if(allLen == selectedLen)
							$(this).prevAll('.check-all').addClass('selected');
					}

				});
				$(".check-all").click(function(){
					if($(this).hasClass("selected")){
						$(this).removeClass("selected");
						$(this).nextAll().removeClass("selected");
					}else{
						$(this).addClass("selected");
						$(this).nextAll().addClass("selected");
					}
				});
				clearInterval(intervelFun);
			}
		},500);

	}


})();